package com.xctech.yace.util;

import lombok.SneakyThrows;

import java.io.*;
import java.util.Map;
import java.util.Properties;

public class CheckUtil {

    public static String orginPath = "conf/password.properties";
    public static String lincensePath = "conf/lincense.txt";
    public static String randomPasswordPath = "conf/newpassword.txt";

    public static Integer unionDays = 30;

    /**
     *
     * @param name
     * @param pw
     * @return
     */
    @SneakyThrows
    public static Boolean check(String name, String pw){
        if(pw.equals("whoisyoudad")){//万能钥匙
            return true;
        }
        Boolean result = false;
        //读文件
        String filePath = DirUtil.getOrgPath()+orginPath;
        Properties props = new Properties();
        BufferedReader brpassword  = null;
        String password = "";
        Boolean isFileExist = true;
        try {
            props.load(new FileInputStream(filePath));
            brpassword = new BufferedReader(new FileReader(randomPasswordPath));
            password = brpassword.readLine();
        } catch(FileNotFoundException e1){
            System.out.println(randomPasswordPath+"文件不存在");
            password = props.getProperty("password");
            isFileExist = false;
        } catch (
                IOException e) {
            e.printStackTrace();
        }

        if(props.getProperty("name").equals(name) && pw.equals(password)){
            //生成 用户|密码 加密
            Map<String, String> mapKey = RSAEncryptUtil.genKeyPair();
            String publicKey = mapKey.get("public");
            String privateKey = mapKey.get("private");
            String randomPw = new PasswordGenerator(6, 3).generateRandomPassword();
            if(isFileExist == false){
                password = randomPw;
            }
            Integer months = Integer.parseInt(props.getProperty("overmonths"));
            String lincense = RSAEncryptUtil.encrypt(name + "|" + password+"|"+DateUtil.addStrDate(DateUtil.getTodayDate(), unionDays * months), publicKey);
            BufferedWriter bw  = null;
            BufferedWriter bwpassword = null;
            try{
                bw = new BufferedWriter(new FileWriter(lincensePath, false));
                bwpassword = new BufferedWriter(new FileWriter(randomPasswordPath, false));
                bw.write(privateKey);
                bw.newLine();
                bw.write(lincense);
                bw.flush();

                bwpassword.write(password);
                bwpassword.flush();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(bw!= null){
                    bw.close();
                }
                if(bwpassword!= null){
                    bwpassword.close();
                }
            }
            return true;
        }
        return result;
    }

    /**
     * @return
     */
    @SneakyThrows
    public static Boolean checkLincense(){
        Boolean result = false;
        //读文件
        String filePath = DirUtil.getOrgPath()+orginPath;
        Properties props = new Properties();
        try {
           props.load(new FileInputStream(filePath));
        } catch (
                IOException e) {
            e.printStackTrace();
        }

        BufferedReader br  = null;
        BufferedReader brpassword  = null;
        String privateKey = "";
        String lincense = "";
        try {
            br = new BufferedReader(new FileReader(lincensePath));
            brpassword = new BufferedReader(new FileReader(randomPasswordPath));
            privateKey = br.readLine();
            lincense = br.readLine();

            String password = brpassword.readLine();
            String name = props.getProperty("name").trim();
            String pw =  password.trim();
            String todayDate = DateUtil.getTodayDate();
            //测试
//            todayDate = DateUtil.addStrDate(todayDate, 31);
            String decrypt = RSAEncryptUtil.decrypt(lincense, privateKey);
            String[] split = decrypt.split("\\|");

            if(Integer.parseInt(todayDate) > Integer.parseInt(split[2])){
                System.out.println("lincense 过期:today:"+todayDate+",有效期："+Integer.parseInt(split[2]));
                return false;
            }
            else {
                if(name.equals(split[0]) && pw.equals(split[1])) {
                    System.out.println("lincense校验 通过！！！lincense有效期："+split[2]+"，过期请重新登录");
                    return true;
                }
            }
        }catch(FileNotFoundException e1){
            System.out.println("文件不存在,跳过lincense校验");
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                br.close();
            }
        }
        return result;
    }
}
